%token ID
%token TIPO	
%token LETRA
%token LITERALINT
%token LITERALSTR
%token COMENTARIO
%token ESPB
%token OPA
%token OPC
%token OPL
%token OPLN
%token PA
%token PF
%token IG
%token CA
%token CF
%token XA
%token XF
%token PT 
%token V
%token PV
%token CLASS 

%%

Goal: 			MainClass ClassDeclaration 
			;

MainClass: 		CLASS Identifier XA "public" "static" "void" "main" PA "String" CA CF Identifier PF XA Statement XF XF
			;

ClassDeclaration: 	CLASS Identifier "extends" Identifier XA VarDeclaration MethodDeclaration XF
			|CLASS Identifier XA VarDeclaration MethodDeclaration XF
			;

VarDeclaration: 	Type Identifier PV
			;

MethodDeclaration: 	"public" Type Identifier PA Type Identifier TypeLoop PF XA  VarDeclaration Statement "return" Expression PV XF
			|"public" Type Identifier PA  PF XA  VarDeclaration Statement "return" Expression PV XF
			;

TypeLoop:		V Type Identifier
			;

Type:		 	TIPO
			|Identifier
			;

Statement:	 	XA Statement XF
			|"if" PA Expression PF Statement "else" Statement
			|"while" PA Expression PF Statement
			|"System.out.println" PA Expression PF PV
			|Identifier IG Expression PV
			|Identifier CA Expression CF IG Expression PV
			;

Expression:	 	Expression OPA Expression
			|Expression OPC Expression
			|Expression OPL Expression
			|Expression CA Expression CF
			|Expression PT "length"
			|Expression PT Identifier PA Expression ExpressionLoop PF
			|Expression PT Identifier PA PF
			|LITERALINT
			|"true"
			|"false"
			|Identifier
			|"this"
			|"new" "int" CA Expression CF
			|"new" Identifier PA PF
			|OPLN Expression
			|PA Expression PF
			;

ExpressionLoop:		V Expression
			;

Identifier:	 	ID
			;

%%


#include "lex.yy.c"

main() {
	yyparse();
	printf("COMPILACAO REALIZADA COM SUCESSO!!\n");
}

yyerror(char *s) {
	printf("\nErro lexico na linha %d, caracter invalido : %s\n, %s\n", 
			yylineno, yytext, s); 
	exit(1);
}
